package com.shjy.client.delegate;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

import com.shjy.client.alipay.AlipayConfig;
import com.shjy.client.util.Const;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wed on 2017/5/20.
 */

public class DetailWebviewDelegate extends BaseWebviewDelegate {

    public DetailWebviewDelegate(Context context, Handler handler, WebView webView) {
        mContext = context;
        mHandler = handler;
        mWebView = webView;
    }

    /**
     * H5调用app支付
     * @param type
     * @param money
     * @param out_trade_no
     */
    @JavascriptInterface
    public void startPhonePay(String type, String money, String out_trade_no) {

        Log.d("pay", "startPhonePay type " + type + ", money " + money + ", out_trade_no " + out_trade_no);

        Map<String, String> map = new HashMap();
        map.put("payType", type);
        map.put("total_amount", money);
        map.put("out_trade_no", out_trade_no);

        Message msg = new Message();
        msg.what = Const.DETAIL_WHAT_PAY;
        msg.obj = map;
        mHandler.sendMessage(msg);
    }

    /**
     * H5调用app支付
     * @param nonce_str
     * @param sign
     * @param prepayid
     */
    @JavascriptInterface
    public void startWeiChatPay(String nonce_str, String sign, String prepayid) {

        Log.d("pay", "startWeiChatPay nonce_str " + nonce_str + ", sign " + sign + ", prepayid " + prepayid);

        Map<String, String> map = new HashMap();
        map.put("payType", Const.PAY_TYPE_WX);
        map.put("nonce_str", nonce_str);
        map.put("sign", sign);
        map.put("prepayid", prepayid);

        Message msg = new Message();
        msg.what = Const.DETAIL_WHAT_PAY;
        msg.obj = map;
        mHandler.sendMessage(msg);
    }

    /**
     * 订单列表页跳转到订单详细页
     * @param waybillId
     */
    @JavascriptInterface
    public void goDetail(final String waybillId) {
        mWebView.post(new Runnable() {
            @Override
            public void run() {
                mWebView.loadUrl(Const.URL_PREFIX + "customer/order/order_detail.html?waybillId=" + waybillId);
            }
        });
    }

    /**
     * 订单列表页点击评价，跳转到评价页面
     * @param waybillId
     */
    @JavascriptInterface
    public void goOrderEvaluation(String waybillId) {

        final String url = Const.URL_PREFIX + "customer/order/order_ok.html?waybillId=" + waybillId;
        mWebView.post(new Runnable() {
            @Override
            public void run() {
                mWebView.loadUrl(url);
            }
        });
    }

    /**
     * 订单列表页点击支付，跳转到支付页
     * @param waybillId
     */
    @JavascriptInterface
    public void goFeeDetail(String waybillId) {

        final String url = Const.URL_PREFIX + "customer/order/price_detail.html?waybillId=" + waybillId;
        mWebView.post(new Runnable() {
            @Override
            public void run() {
                mWebView.loadUrl(url);
            }
        });
    }
}
